package com.chris.homecare.screens.events
{
	import com.chris.homecare.components.CalenderDateComponent;
	import com.chris.homecare.components.EventComponent;
	import com.chris.homecare.data.DataUtil;
	import com.chris.homecare.data.EventData;
	import com.chris.homecare.utils.CommonUtils;
	
	import feathers.controls.Label;
	import feathers.controls.LayoutGroup;
	import feathers.controls.Screen;
	import feathers.controls.ScrollContainer;
	import feathers.layout.AnchorLayout;
	import feathers.layout.AnchorLayoutData;
	import feathers.layout.HorizontalLayout;
	import feathers.layout.TiledRowsLayout;
	import feathers.layout.VerticalLayout;
	
	import starling.display.Quad;
	import starling.events.Event;
	
	public class CalenderScreen extends Screen
	{
		private var currentActiveItem:CalenderDateComponent;
		private var calenderGroup:LayoutGroup;
		private var calenderDaysArray:Array = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
		
		private var daysArray:Array;
		private var weekCompArray:Array = new Array();
		private var daysCompArray:Array = new Array();

		private var scrollContainer:ScrollContainer;

		private var eventHeadingBackground:Quad;

		private var eventListGroup:LayoutGroup;

		private var eventDatesArray:Array;
		
		public function CalenderScreen()
		{
			setupDateData();
			addEventListener(CalenderDateComponent.TRIGGER_ACTIVE, onDatePicked);
		}
		
		public function repopulateDates():void
		{
			if(!calenderGroup) return;
			
			calenderGroup.removeChildren(0, -1, true);
			
			setupDateData();
			setupDates();
			this.invalidate(INVALIDATION_FLAG_LAYOUT);
		}
		
		override protected function initialize():void
		{
			scrollContainer = new ScrollContainer();
			scrollContainer.horizontalScrollPolicy = ScrollContainer.SCROLL_POLICY_OFF;
			addChild(scrollContainer);
			
			scrollContainer.layout = new VerticalLayout();
			
			var weekGroup:LayoutGroup = new LayoutGroup();
			var weekLayout:HorizontalLayout = new HorizontalLayout();
			weekLayout.gap = 1;
			weekLayout.paddingLeft = 3.5;
			weekLayout.paddingBottom = 1;
			weekGroup.layout = weekLayout;
			scrollContainer.addChild(weekGroup);
			
			for(var i:int=0; i<7; i++)
			{
				var weekDaysComp:CalenderDateComponent;
				if(i==0) weekDaysComp = new CalenderDateComponent(calenderDaysArray[i], CalenderDateComponent.SUNDAY);
				else weekDaysComp = new CalenderDateComponent(calenderDaysArray[i],  CalenderDateComponent.OTHERDAYS);
				weekDaysComp.isEnabled = false;
				weekGroup.addChild(weekDaysComp);
				weekCompArray.push(weekDaysComp);
			}
			
			calenderGroup = new LayoutGroup();
			var calenderLayout:TiledRowsLayout = new TiledRowsLayout();
			calenderLayout.gap = 1;
			calenderLayout.paddingBottom = 2;
			calenderGroup.layout = calenderLayout;
			calenderLayout.useSquareTiles = false;
			scrollContainer.addChild(calenderGroup);
			
			var eventHeading:LayoutGroup=new LayoutGroup();
			eventHeading.layout = new AnchorLayout();
			scrollContainer.addChild(eventHeading);
			
			eventListGroup = new LayoutGroup();
			eventListGroup.layout = new VerticalLayout();
			scrollContainer.addChild(eventListGroup);
			
			eventHeadingBackground = new Quad(1,72*CommonUtils.scaleValue,0x64a950);
			eventHeading.addChild(eventHeadingBackground);
			
			var eventHeadingLabel:Label = new Label();
			eventHeadingLabel.text = "Events";
			eventHeadingLabel.nameList.add("grouplist-head-label");
			eventHeading.addChild(eventHeadingLabel);
			var eventHeadLblLayoutData:AnchorLayoutData = new AnchorLayoutData();
			eventHeadLblLayoutData.horizontalCenter = eventHeadLblLayoutData.verticalCenter = 0;
			eventHeadingLabel.layoutData = eventHeadLblLayoutData;
			
			setupDates();
			selectCurrentDate();
			processSelectedDay();
			super.initialize();
		}
		
		private function selectCurrentDate():void
		{
			var firstDate:Date = daysArray[0];
			var currentDate:Date = new Date();
			currentActiveItem = daysCompArray[firstDate.day + currentDate.date -1];
			currentActiveItem.replaceBackground(CalenderDateComponent.SELECTED);
		}
		
		override protected function draw():void
		{
			var stageWidth:int = stage.stageWidth;
			var stageHeight:int = stage.stageHeight;
			
			scrollContainer.width = calenderGroup.width = eventHeadingBackground.width = stageWidth;
			scrollContainer.height = this.height;
			
			for(var i:int=0; i<7; i++)
			{
				weekCompArray[i].width = (stageWidth - 6)/7 -1;
			}
			
			for(var j:int=0; j<daysCompArray.length; j++)
			{
				daysCompArray[j].width = (stageWidth - 6)/7 - 1;
				daysCompArray[j].height = (stageWidth - 6)/7 - 24*CommonUtils.scaleValue;
			}
			
			super.draw();
		}
		
		private function setupDateData():void
		{
			var firstDate:Date = new Date(CommonUtils.eventCurrentYear, CommonUtils.eventCurrentMonth, 1);
			var lastDate:Date = new Date(CommonUtils.eventCurrentYear, CommonUtils.eventCurrentMonth+1, 0);
			
			daysArray = new Array();
			
			for(var i:int=1; i<=lastDate.date; i++)
			{
				daysArray.push(new Date(CommonUtils.eventCurrentYear, CommonUtils.eventCurrentMonth, i));
			}
			
			generateEventDates(firstDate, lastDate);
		}
		
		private function setupDates():void
		{
			var len:int = daysArray.length;
			var firstDate:Date = daysArray[0];
			var lastDate:Date = daysArray[daysArray.length-1];
			
			var prevMonthStartDate:Date = new Date(firstDate.fullYear, firstDate.month, firstDate.date-firstDate.day);
			var prevMonthDate:int = prevMonthStartDate.date;
			
			var calDateComp:CalenderDateComponent;
			var i:int;
			for(i=0; i<firstDate.day; i++)
			{
				calDateComp = new CalenderDateComponent(String(prevMonthDate+i), CalenderDateComponent.DISABLED);
				calDateComp.isEnabled = false;
				calenderGroup.addChild(calDateComp);
				daysCompArray.push(calDateComp);
			}
			
			for(i=0; i<len; i++)
			{
				var dateStr:String = (String(daysArray[i].date).length > 1)? String(daysArray[i].date) : "0"+String(daysArray[i].date);
				
				var hasEventValue:Boolean = false;
				//if(eventDatesArray[i] && eventDatesArray[i] == 1) hasEventValue = true; 
				if(eventDatesArray[i] && eventDatesArray[i] == 1) 
				{
					var currentDate:Date = new Date();
					currentDate.setMilliseconds(0);
					currentDate.setSeconds(0);
					currentDate.setMinutes(0);
					currentDate.setHours(0);
					if(daysArray[i] < currentDate)
					{
						calDateComp = new CalenderDateComponent(dateStr, CalenderDateComponent.DESELECTED, true, true);
					}
					
					else 
					{
						calDateComp = new CalenderDateComponent(dateStr, CalenderDateComponent.DESELECTED, true, false);
					}
				}
				else calDateComp = new CalenderDateComponent(dateStr);
				calDateComp.activeDate = daysArray[i];
				calenderGroup.addChild(calDateComp);
				daysCompArray.push(calDateComp);
			}
			
			for(i=lastDate.day; i<6; i++)
			{
				calDateComp = new CalenderDateComponent("0"+(i - lastDate.day + 1), CalenderDateComponent.DISABLED);
				calDateComp.isEnabled = false;
				calenderGroup.addChild(calDateComp);
				daysCompArray.push(calDateComp);
			}
		}
		
		private function generateEventDates(firstDate:Date, lastDate:Date):void
		{
			var tempArray:Array = DataUtil.getAllEventsDatesForTheMonth(firstDate, lastDate);
			var eventsLen:uint = tempArray? tempArray.length: 0;
			
			eventDatesArray = new Array();
			
			for(var i:int=0; i<eventsLen; i++)
			{
				eventDatesArray[tempArray[i].eventDate-1] = true;
			}
		}
		
		private function onDatePicked(event:Event):void
		{
			if(currentActiveItem)
				currentActiveItem.replaceBackground();
			
			if(currentActiveItem != event.target)
			{
				currentActiveItem = CalenderDateComponent(event.target);
				processSelectedDay();
			}
				
			else currentActiveItem = null;
		}
		
		private function processSelectedDay():void
		{
			eventListGroup.removeChildren(0,-1,true);
			var eventsArray:Array = DataUtil.getAllEventsFortheDay(currentActiveItem.activeDate);
			var numEvents:int = eventsArray.length;
			for(var i:int=0; i<numEvents; i++)
			{
				var eventData:EventData = eventsArray[i];
				var eventComp:EventComponent = new EventComponent(eventData);
				eventListGroup.addChild(eventComp);
			}
			
		}
	}
}